﻿


$(function () {
    var modelApi = getBaseplatformPort () 
    // 点击立即注册
    $('#registerBtn').on('click', function () {
        var userName = $('#nichengName').val();
        if (!userName) {
            layer.msg('请输入昵称', {
                shade: 0.3,
                time: 1000
            });
            return;
        }
        var Name = $('#xingmingName').val();
        if (!Name) {
            layer.msg('请输入姓名', {
                shade: 0.3,
                time: 1000
            });
            return;
        }
        var passwordInput = $('#passwordInput').val();
        if (!passwordInput) {
            layer.msg('请输入密码', {
                shade: 0.3,
                time: 1000
            });
            return;
        }
        var rePasswordInput = $('#rePasswordInput').val();
        if (passwordInput != rePasswordInput) {
            layer.msg('请确认两次输入密码一致', {
                shade: 0.3,
                time: 1000 // 1秒关闭
            });
            return;
        }
        var tel = $('#shoujihao').val();
        if (!tel) {
            layer.msg('请输入手机号', {
                shade: 0.3,
                time: 1000
            })
            return;
        }
        var email = $('#email').val();
        if (!email) {
            layer.msg('请输入邮箱', {
                shade: 0.3,
                time: 1000
            })
            return;
        }
        var duties = $('#txt_zhiwu').val();
        if (!duties) {
            // layer.msg('请输入职务', {
            //     shade: 0.3,
            //     time: 1000
            // })
            // return;
        }
        var department = $('#orgInput').attr('checkid');
        if (!department) {
            layer.msg('请选择机构', {
                shade: 0.3,
                time: 1000
            })
            return;
        }
        $.ajax({
            type: "post",
            async: false,
            url: modelApi + "/user/insertUser",
            data: {
                userName: userName,
                no: userName,
                name: Name,
                pass: passwordInput,
                tel: tel,
                email: email,
                fkDuty: duties,
                fkDept: department,
                ismanager: 0
            },
            datatype: 'json',
            success: function (res) {
                res = JSON.parse(res);
                if (res.code == 200) {
                    layer.msg(res.msg, {
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        window.location.href = "/Login";
                    });
                } else {
                    layer.msg(res.msg, {
                        shade: 0.3,
                        time: 1000 // 1秒关闭
                    });
                    return;
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
    });
    $('#nichengName, #xingmingName, #passwordInput, #rePasswordInput,#shoujihao,#email,#txt_zhiwu,#orgInput').on('blur', function () {
        if (!valueCheck.checkUnallowable(this.value)) {
            this.value = '';
            layer.msg('请勿输入特殊字符或关键字', {
                shade: 0.3,
                time: 1000 // 1秒关闭
            });
            return;
        }
    });
    $('#shoujihao').on('blur', function () {
        if (this.value && !valueCheck.checkPhone(this.value)) {
            this.value = '';
            layer.msg('请输入正确的手机号', {
                shade: 0.3,
                time: 1000 // 1秒关闭
            });
            return;
        }
    });
    // 手机号数字验证
    $('#shoujihao').on('input keyup', function () {
        this.value = this.value.replace(/[^\d]/g, '');
    });
    $('#email').on('blur', function () {
        if (this.value && !valueCheck.checkMail(this.value)) {
            this.value = '';
            layer.msg('请输入正确的邮箱', {
                shade: 0.3,
                time: 1000 // 1秒关闭
            });
        }
    });

    // 点击选择部门
    $('#orgInput').on('click', function () {
        if ($('#ztreeDiv').is(':hidden')) {
            $('#ztreeDiv').show()
        } else {
            $('#ztreeDiv').hide();
        }
    });
    jQuery.fn.isChildOf = function (b) {
        return (this.parents(b).length > 0);
    };
    // 重置
    $('#resetBtn').on('click', function () {
        $('#nichengName,#xingmingName,#passwordInput,#rePasswordInput,#shoujihao,#email,#txt_zhiwu,#orgInput').val('');
    });

    var valueCheck = {
        // 检测非法字符,防止SQL注入
        checkUnallowable: function (value) {
            var re = /select|update|delete|exec|count|’|"|=|;|>|<|%/i;
            if (re.test(value)) {
                return false;
            } else {
                return true;
            }
        },
        // 检测手机号格式
        checkPhone: function (phone) {
            if ((/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
                return true;
            } else {
                return false;
            }
        },
        //邮箱
        checkMail: function (mail) {
            if ((/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(mail))) {
                return true;
            } else {
                return false;
            }
        }
    }


    // 根节点id(自定义)
    var rootNodeId = '101010';

    var settingLeft = {
        view: {
            dblClickExpand: false, //双击节点时，是否自动展开父节点的标识
            showLine: false, //是否显示节点之间的连线
            showIcon: true,
            fontCss: {
                'color': 'white',
                'font-weight': 'normal'
            }, //字体样式函数
            selectedMulti: false //设置是否允许同时选中多个节点
        },
        check: {
            //chkboxType: { "Y": "ps", "N": "ps" },
            chkStyle: "checkbox", //复选框类型
            enable: false //每个节点上是否显示 CheckBox
        },
        data: {
            simpleData: { //简单数据模式
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode) {
                zTree = $.fn.zTree.getZTreeObj("menu_tree");
                if ($(event.target).hasClass('ico_close') || $(event.target).hasClass('ico_open')) {
                    zTree.expandNode(treeNode); //如果是父节点，则展开该节点
                }
                var text = $(event.target).text();
                var id = treeNode.id;
                console.log(id);
                console.log(text);

                if (text) {
                    $('#orgInput').val(text);
                    $('#orgInput').attr('checkid', id);
                    $('#ztreeDiv').hide();
                }
            },
            onDblClick: function (treeId, treeNode) {
                //code
            }
        }
    };
    // 获取机构组织数据
    var zNodes;
    $.ajax({
        type: "post",
        async: false,
        // url: "/BaseAshx/Org/OrgAshx.ashx",
        // data: {
        //     stype: 'OrgManage_QueryOrgTree',
        //     orgid: rootNodeId,
        //     treetype: 'down'
        // },
        url: modelApi + 'dept/getAllDepts',
        data: {
            orgid: rootNodeId,
            treetype: 'down'
        },
        datatype: "json",
        success: function (result) {
            result = JSON.parse(result);
            if (result.code == 200) {
                var data = result.data;
                zNodes = data.map(function (item, index) {
                    // console.log(item);
                    var mapResult = {
                        "id": item.ORG_ID,
                        "name": item.ORGSHORTNAME,
                        "pId": item.ORG_PID,
                    };
                    if (index == 0) {
                        mapResult.open = true;
                    }
                    return mapResult;
                });
                //初始化菜单树
                $.fn.zTree.init($("#menu_tree"), settingLeft, zNodes);
            } else {
                layer.msg('获取机构数据错误,请稍候再试', {
                    shade: 0.3,
                    time: 1000 // 1秒关闭
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
})